//
//  PaperItemSubjectiveAnalysisCell.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/9.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import SnapKit
import Then

///简答题  解析cell
class PaperItemSubjectiveAnalysisCell: UITableViewCell {
    var answerLabel: UILabel!
    var myAnswerLabel: UILabel!
    var analysisLabel: UILabel!
    var startRateView: LSFStarRateView!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func set(answers: String?,
                    myAnswers: String?,
                    analysis: String?,
                    difficulty: Int?) {

        answerLabel.lsf_htmlParagraphTextAndFitSize(text: answers,
                                                    width: k_screen_width - 80,
                                                    firstLineHeadIndent: 30)
        
        myAnswerLabel.lsf_htmlParagraphTextAndFitSize(text: myAnswers,
                                                      width: k_screen_width - 80,
                                                      firstLineHeadIndent: 30)
        analysisLabel.lsf_htmlParagraphTextAndFitSize(text: analysis,
                                                      width: k_screen_width - 80,
                                                      firstLineHeadIndent: 30)
        
        if difficulty != nil {
            startRateView.scorePercent = Float(difficulty!) / 5.0
        }
        else {
            startRateView.scorePercent = 0
        }
    }
    
    private func setup() {
        contentView.backgroundColor = .clear
        self.backgroundColor = .clear
        
        let topLine = UIView().then { (view) in
            view.backgroundColor = Config.default.grayLineViewColor
            
            contentView.addSubview(view)
            view.snp.makeConstraints({ (make) in
                make.leading.trailing.equalToSuperview()
                make.top.equalToSuperview().offset(15)
                make.height.equalTo(1)
            })
        }
        

        var titleLabel = UILabel().then { (label) in
            label.text = "正确答案"
            label.font = UIFont.systemFont(ofSize: 14)
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.top.equalTo(topLine.snp.bottom).offset(10)
                maker.leading.equalTo(10)
                maker.height.equalTo(15)
            })
        }
        
        answerLabel = UILabel().then({ (label) in
            label.font = UIFont.systemFont(ofSize: 14)
            label.numberOfLines = 0
            label.textColor = UIColor.lsf_color16(0x19C585)
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.top.equalTo(titleLabel.snp.bottom).offset(3)
                maker.leading.equalTo(10)
                maker.trailing.equalTo(-10)
            })
        })
        
        titleLabel = UILabel().then { (label) in
            label.text = "我的答案"
            label.textColor = Config.default.themeColor
            label.font = UIFont.systemFont(ofSize: 14)
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.top.equalTo(answerLabel.snp.bottom).offset(10)
                maker.leading.equalTo(10)
                maker.height.equalTo(15)
            })
        }
        
        myAnswerLabel = UILabel().then({ (label) in
            label.font = UIFont.systemFont(ofSize: 14)
            label.numberOfLines = 0
            label.textColor = Config.default.themeColor
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.top.equalTo(titleLabel.snp.bottom).offset(3)
                maker.leading.equalTo(10)
                maker.trailing.equalTo(-10)
            })
        })
        
        
        
        titleLabel = UILabel().then { (label) in
            label.text = "解析:"
            label.font = UIFont.systemFont(ofSize: 14)
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.top.equalTo(myAnswerLabel.snp.bottom).offset(10)
                maker.leading.equalTo(10)
                maker.height.equalTo(15)
            })
        }
        
        analysisLabel = UILabel().then({ (label) in
            label.font = UIFont.systemFont(ofSize: 14)
            label.numberOfLines = 0
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.top.equalTo(titleLabel.snp.bottom).offset(3)
                maker.leading.equalTo(10)
                maker.trailing.equalTo(-10)
            })
        })
        
        let diffLabel = UILabel().then { (label) in
            label.text = "难度:"
            label.font = UIFont.systemFont(ofSize: 14)
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.leading.equalTo(10)
                maker.top.equalTo(analysisLabel.snp.bottom).offset(14)
            })
        }
        
        startRateView = LSFStarRateView().then({ (view) in
            view.bounds = CGRect.init(x: 0, y: 0, width: 142, height: 22)
            view.setBottomImageName("star", topImgName: "start_fill", count: 5)
            
            contentView.addSubview(view)
            view.snp.makeConstraints({ (maker) in
                maker.size.equalTo(CGSize.init(width: 142, height: 22))
                maker.leading.equalTo(diffLabel.snp.trailing).offset(5)
                maker.centerY.equalTo(diffLabel.snp.centerY)
                maker.bottom.equalTo(-10)
            })
            
        })
    }
}
